package com.zuper.demo.sessionlogin.exception;

import com.zuper.demo.sessionlogin.constant.ErrorEnum;

public class BusinessException extends RuntimeException{
	private static final long serialVersionUID = 1L;
	
	private String code;
	private String msg;
	
	public BusinessException(String errMsg) {
		super(errMsg);
		this.msg = errMsg;
	}
	
	public BusinessException(Throwable cause) {
		super(cause);
		this.msg = cause.getMessage();
	}

	public BusinessException(String errMsg, Throwable cause) {
		super(errMsg, cause);
		this.msg = errMsg;
	}

	/**
	 * 可传多个参数，结合String格式化占位符
	 * format占位符：
	 * %s -字符串
	 * %d -整形（十进制）
	 * %c -字符型
	 * %f -浮点型
	 * @param errMsg
	 * @param params
	 */
	public BusinessException(String errMsgFmt, Object ...params) {
		super(errMsgFmt = String.format(errMsgFmt, params));
		this.msg = errMsgFmt;
	}

	public BusinessException(ErrorEnum enm) {
		super(enm.code() + " " + enm.msg());
		this.code = enm.code();
		this.msg = enm.msg();
	}
	
	public String code() {
		return code;
	}

	public String msg() {
		return msg;
	}
}
